<?php
namespace app\api\model;

use think\Model;
use think\exception\ValidateException;

class Courier extends Model
{
    protected $name = 'courier';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 配送员新增
     */
    public function courierSave($param)
    {
        // 验证数据
        try {
            validate([
                'appid'  => 'require', 
                'third_number'  => 'require', 
            ])->message([
                'appid.require'  => 'appid必须传递',   
                'third_number.require'  => 'third_number必须传递',    
            ])->check($param);
        } catch (ValidateException $e) {
            return result_error($e->getError());
        }

        $data['appid'] = $param['appid'] ?? '';//appid
        $data['third_number'] = $param['third_number'] ?? '';//第三方配送平台订单号
        $data['sys_number'] = $param['sys_number'] ?? '';//系统订单号(系统生成)
        $data['call_number'] = $param['call_number'] ?? '';//调用方订单号
        $data['app_type'] = $param['app_type'] ?? '';//第三方平台类型 1:闪送, 2:达达,3:蜂鸟
        $data['latitude'] =$param['latitude']?? '';//配送员位置纬度（百度坐标系）
        $data['longitude'] =$param['longitude']?? '';//配送员位置经度（百度坐标系）
        $data['name'] = $param['name'] ?? '';//配送员姓名
        $data['mobile'] = $param['mobile'] ?? '';//配送员手机号
        $data['time'] = $param['time'] ?? '';//配送员位置所处的当前时间
        $data['order_count'] = $param['order_count'] ?? '';//配送员服务总次数
        $data['head_icon'] = $param['head_icon'] ?? '';//配送员头像
        $data['rider_id'] = $param['rider_id'] ?? '';//第三方配送平台骑手id
        if(isset($param['finish_code'])) $data['finish_code'] = $param['finish_code'] ?? '';//收货码
        if(isset($param['delivery_process_trail'])) $data['delivery_process_trail'] = json_encode($param['delivery_process_trail']) ?? '';//配送过程轨迹列表
        if(isset($param['estimate_delivery_time_tip'])) $data['estimate_delivery_time_tip'] = json_encode($param['estimate_delivery_time_tip']) ?? '';//预计送达时间文案,配送员未接单或者订单终态，该值为空

        try {
            $find = $this
                ->when(!empty($param['third_number']),function ($query) use($param) {
                    $query->where('third_number', $param['third_number']);
                })
                ->when(!empty($param['sys_number']),function ($query) use($param) {
                    $query->where('sys_number', $param['sys_number']);
                })
                ->when(!empty($param['call_number']),function ($query) use($param) {
                    $query->where('call_number', $param['call_number']);
                })
                ->find();
            if (!empty($find)){
                $result = $find->save($data);
            }else{
                $result = $this->save($data);
            }
           if ($result) {
                return result_success('success',$this->id);
            } else {
                return result_error('保存失败');
            }
        } catch (\Exception $e) {
            return result_error($e->getMessage());
        }
    }

    /**
     * 配送员更新
     */
    public function courierUpdate($param)
    {
        // 验证数据
         if (empty($param['third_number'])&&empty($param['sys_number'])&&empty($param['call_number'])&&empty($param['id'])){
                return  result_error('缺少必要更新条件');
        }
        
        if(isset($param['latitude'])) $data['latitude'] = $param['latitude'];//配送员位置纬度（百度坐标系）
        if(isset($param['longitude'])) $data['longitude'] = $param['longitude'];//配送员位置经度（百度坐标系）
        if(isset($param['name'])) $data['name'] = $param['name'];//配送员姓名
        if(isset($param['mobile'])) $data['mobile'] = $param['mobile'];//配送员手机号
        if(isset($param['time'])) $data['time'] = $param['time'];//配送员位置所处的当前时间
        if(isset($param['order_count'])) $data['order_count'] = $param['order_count'];//配送员服务总次数
        if(isset($param['head_icon'])) $data['head_icon'] = $param['head_icon'];//配送员头像
        if(isset($param['rider_id'])) $data['rider_id'] = $param['rider_id'];//第三方配送平台骑手id
        if(isset($param['finish_code'])) $data['finish_code'] = $param['finish_code'] ?? '';//收货码
        if(isset($param['delivery_process_trail'])) $data['delivery_process_trail'] = $param['delivery_process_trail'];//配送过程轨迹列表
        if(isset($param['estimate_delivery_time_tip'])) $data['estimate_delivery_time_tip'] = $param['estimate_delivery_time_tip'];//预计送达时间文案,配送员未接单或者订单终态，该值为空


        try {
            $find = $this
                    ->when(!empty($param['third_number']),function ($query) use($param) {
                        $query->where('third_number', $param['third_number']);
                    })
                    ->when(!empty($param['sys_number']),function ($query) use($param) {
                        $query->where('sys_number', $param['sys_number']);
                    })
                    ->when(!empty($param['call_number']),function ($query) use($param) {
                        $query->where('call_number', $param['call_number']);
                    })
                    ->when(!empty($param['id']),function ($query) use($param) {
                        $query->where('id', $param['id']);
                    })
                    ->find();
            if (!$find){
                return  result_error('未查询到配送员');
            }  
            
            $result=$find->save($data);

           if ($result) {
                return result_success('success',$result);
            } else {
                return result_error('保存失败');
            }
        } catch (\Exception $e) {
            return result_error($e->getMessage());
        }
    }
}
